<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="资料名称" prop="dName">
      <el-input v-model="dataForm.dName" placeholder="资料名称"></el-input>
    </el-form-item>
    <el-form-item label="资料分类" prop="dType">
      <el-input v-model="dataForm.dType" placeholder="资料分类"></el-input>
    </el-form-item>
    <el-form-item label="文件类型" prop="dFiletype">
      <el-input v-model="dataForm.dFiletype" placeholder="文件类型"></el-input>
    </el-form-item>
    <el-form-item label="文件上传" prop="dFileupload">
      <el-input v-model="dataForm.dFileupload" placeholder="文件上传"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="dRemarks">
      <el-input v-model="dataForm.dRemarks" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="发布者" prop="dPublisher">
      <el-input v-model="dataForm.dPublisher" placeholder="发布者"></el-input>
    </el-form-item>
    <el-form-item label="发布日期" prop="dDate">
      <el-input v-model="dataForm.dDate" placeholder="发布日期"></el-input>
    </el-form-item>
    <el-form-item label="更新日期" prop="dNewdate">
      <el-input v-model="dataForm.dNewdate" placeholder="更新日期"></el-input>
    </el-form-item>
    <el-form-item label="下载" prop="dDownload">
      <el-input v-model="dataForm.dDownload" placeholder="下载"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          dId: 0,
          dName: '',
          dType: '',
          dFiletype: '',
          dFileupload: '',
          dRemarks: '',
          dPublisher: '',
          dDate: '',
          dNewdate: '',
          dDownload: ''
        },
        dataRule: {
          dName: [
            { required: true, message: '资料名称不能为空', trigger: 'blur' }
          ],
          dType: [
            { required: true, message: '资料分类不能为空', trigger: 'blur' }
          ],
          dFiletype: [
            { required: true, message: '文件类型不能为空', trigger: 'blur' }
          ],
          dFileupload: [
            { required: true, message: '文件上传不能为空', trigger: 'blur' }
          ],
          dRemarks: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          dPublisher: [
            { required: true, message: '发布者不能为空', trigger: 'blur' }
          ],
          dDate: [
            { required: true, message: '发布日期不能为空', trigger: 'blur' }
          ],
          dNewdate: [
            { required: true, message: '更新日期不能为空', trigger: 'blur' }
          ],
          dDownload: [
            { required: true, message: '下载不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.dId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.dId) {
            this.$http({
              url: this.$http.adornUrl(`/com.emp/datebaseinformation/info/${this.dataForm.dId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.dName = data.datebaseInformation.dName
                this.dataForm.dType = data.datebaseInformation.dType
                this.dataForm.dFiletype = data.datebaseInformation.dFiletype
                this.dataForm.dFileupload = data.datebaseInformation.dFileupload
                this.dataForm.dRemarks = data.datebaseInformation.dRemarks
                this.dataForm.dPublisher = data.datebaseInformation.dPublisher
                this.dataForm.dDate = data.datebaseInformation.dDate
                this.dataForm.dNewdate = data.datebaseInformation.dNewdate
                this.dataForm.dDownload = data.datebaseInformation.dDownload
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/com.emp/datebaseinformation/${!this.dataForm.dId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'dId': this.dataForm.dId || undefined,
                'dName': this.dataForm.dName,
                'dType': this.dataForm.dType,
                'dFiletype': this.dataForm.dFiletype,
                'dFileupload': this.dataForm.dFileupload,
                'dRemarks': this.dataForm.dRemarks,
                'dPublisher': this.dataForm.dPublisher,
                'dDate': this.dataForm.dDate,
                'dNewdate': this.dataForm.dNewdate,
                'dDownload': this.dataForm.dDownload
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
